#include "light.h"

static light_app_t *local_app;

extern lv_indev_t *my_indev;
extern app_t curr_page;
extern list_app_t list_app;

LV_FONT_DECLARE(SourceHanSansSC_22);

void remove_light_app()
{
}

static void slider_event_cb(lv_event_t *e)
{
    lv_obj_t *slider = lv_event_get_target(e);
    char buf[8];
    lv_snprintf(buf, sizeof(buf), "%d", (int)lv_slider_get_value(slider));
    lv_label_set_text(local_app->slider_label, buf);

    ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, (int)lv_slider_get_value(slider) * 500));
    // Update duty to apply the new value
    ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
}

static void event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_CLICKED)
    {
        remove_light_app();
        create_list_app(&list_app);
    }
}

static void event_handler_dropdown(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj = lv_event_get_target(e);

    if (code == LV_EVENT_VALUE_CHANGED)
    {
        char buf[32];
        lv_dropdown_get_selected_str(obj, buf, sizeof(buf));
        if (strcmp(buf, "便携模式") == 0)
        {
            char *set_mode_str = "$PCAS11,0*1D\r\n";
            uart_write_bytes(UART_NUM_1, (const char *)set_mode_str, strlen(set_mode_str));
        }
        else if (strcmp(buf, "步行模式") == 0)
        {
            char *set_mode_str = "$PCAS11,2*1F\r\n";
            uart_write_bytes(UART_NUM_1, (const char *)set_mode_str, strlen(set_mode_str));
        }
        else if (strcmp(buf, "车载模式") == 0)
        {
            char *set_mode_str = "$PCAS11,3*1E\r\n";
            uart_write_bytes(UART_NUM_1, (const char *)set_mode_str, strlen(set_mode_str));
        }
    }
}

void create_light_app(light_app_t *app)
{
    local_app = app;
    curr_page = LIGHT_APP;

    lv_obj_t *scr = lv_obj_create(NULL);
    lv_obj_set_layout(scr, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(scr, LV_FLEX_FLOW_COLUMN);

    lv_group_t *g = lv_group_create();
    lv_group_set_default(g);
    lv_indev_set_group(my_indev, g);

    uint32_t duty = ledc_get_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0);

    lv_obj_t *slider = lv_slider_create(scr);
    lv_slider_set_range(slider, 1, 16);
    lv_slider_set_value(slider, duty / 500, LV_ANIM_OFF);
    lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

    /*Create a label below the slider*/
    local_app->slider_label = lv_label_create(scr);
    lv_label_set_text_fmt(local_app->slider_label, "%d", duty / 500);

    lv_obj_t *dd = lv_dropdown_create(scr);
    lv_obj_set_style_text_font(dd, &SourceHanSansSC_22, LV_PART_MAIN);
    lv_dropdown_set_options(dd, "步行模式\n"
                                "便携模式\n"
                                "车载模式");
    lv_obj_add_event_cb(dd, event_handler_dropdown, LV_EVENT_ALL, NULL);

    lv_obj_t *list = lv_dropdown_get_list(dd);
    if (list != NULL)
    {
        lv_obj_set_style_text_font(list, &SourceHanSansSC_22, LV_PART_MAIN);
    }

    lv_obj_t *btn1 = lv_btn_create(scr);
    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);

    lv_obj_t *btn1_label = lv_label_create(btn1);
    lv_label_set_text(btn1_label, "Exit");

    lv_scr_load_anim(scr, LV_SCR_LOAD_ANIM_MOVE_TOP, 300, 0, true);
}